//
//  photoView.m
//  FaceRecognition
//
//  Created by zz on 16/12/1.
//  Copyright © 2016年 Sylar. All rights reserved.
// 

#import "photoView.h"

@implementation photoView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        _photoImageView = [[UIImageView alloc] init];
        _photoImageView.clipsToBounds = YES;
        _photoImageView.contentMode = UIViewContentModeScaleAspectFill;
        _photoImageView.image = [UIImage imageNamed:@"information_upload_photos_"];
        _photoImageView.userInteractionEnabled = YES;

        
        _photoImageView.image = [UIImage imageNamed:@"information_upload_photos_"];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAction:)];
                                                                                                           
        [_photoImageView addGestureRecognizer:tap];
        [self addSubview:_photoImageView];
        
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectBtn setBackgroundColor:DEFAULTCOLOR];
        
        [_selectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_selectBtn setTitle:@"点击上传" forState:UIControlStateNormal];
        _selectBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_selectBtn];
        
        _photoImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.width - 24);
        _selectBtn.frame = CGRectMake(0, CGRectGetMaxY(_photoImageView.frame), self.frame.size.width, 24);
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_photoImageView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _photoImageView.bounds;
        maskLayer.path = maskPath.CGPath;
        _photoImageView.layer.masksToBounds = YES;
        _photoImageView.layer.mask = maskLayer;
        
        UIBezierPath * maskBtnPath = [UIBezierPath bezierPathWithRoundedRect:_selectBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer *maskBtnLayer = [[CAShapeLayer alloc] init];
        maskBtnLayer.frame = _selectBtn.bounds;
        maskBtnLayer.path = maskBtnPath.CGPath;
        _selectBtn.layer.mask = maskBtnLayer;

    }
    
    return self;
}

- (void)setSelectImage:(UIImage *)selectImage{
    
    if (selectImage) {
        
        _selectImage = selectImage;
        _photoImageView.image = _selectImage;
        
        [_selectBtn setTitle:@"点击更换" forState:UIControlStateNormal];
        
    }else{
        
        
        
        [_selectBtn setTitle:@"点击上传" forState:UIControlStateNormal];
        
        _selectImage = nil;
        _photoImageView.image = [UIImage imageNamed:@"information_upload_photos_"];
    }

}

- (instancetype)init{
    
    return [self initWithFrame:CGRectZero];
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    
}

- (void)selectAction:(id)sender{
    
    if ([_delegate respondsToSelector:@selector(selectPhoto:)]) {
        
        [_delegate selectPhoto:self];
    }
}

@end
